<?php
class ControllerDeployBasic extends Controller {
    public function index() {


    $this->data['super_password']=  "";

        $this->data['button_continue']=$this->language->get('button_continue');
    $this->data['regions']=array(
        array(
            "value"=>"qingcloud_pek2",
            'name'  =>"青云北京2区"
            ),
        array(
            "value"=>"qingcloud_gd1",
            'name'  =>"青云广东1区"
            ),
        );
    
        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/deploy/basic.tpl')) {
            $this->template = $this->config->get('config_template') . '/template/deploy/basic.tpl';
        } else {
            $this->template = 'default/template/deploy/basic.tpl';
        }
    $this->response->setOutput($this->render());
    }


    public function validate() {
        $this->language->load('deploy/basic');

        $json = array();

        // Validate if customer is logged in.
        if (!$this->customer->isLogged()) {
            $json['redirect'] = $this->url->link('checkout/checkout', '', 'SSL');
        }

        // Validate cart has products and has stock.
        if ((!$this->cart->hasProducts() && empty($this->session->data['vouchers'])) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) {
            $json['redirect'] = $this->url->link('checkout/cart');
        }

        // Validate minimum quantity requirments.
        $products = $this->cart->getProducts();

        foreach ($products as $product) {



            $product_total = 0;

            foreach ($products as $product_2) {
                if ($product_2['product_id'] == $product['product_id']) {
                    $product_total += $product_2['quantity'];
                }
            }

            if ($product['minimum'] > $product_total) {
                $json['redirect'] = $this->url->link('checkout/cart');
                break;
            }
        }

        if (!$json) {
        	if (!$this->check->check_str_len($this->request->post['super_password'], 8, 32) || (!$this->check->check_up_low_number_exist($this->request->post['super_password']))) {
                $json['error']['super_password'] = $this->language->get('error_super_password');
            }

//            if (!$this->check->check_cloud_region($this->request->post['region'])){
//            	$json['error']['region'] = "the input region is not valid";
//            }
        }

        if (!$json) {
            $this->session->data['app']['basic']['super_password'] = trim($this->request->post['super_password']);
            $this->session->data['app']['basic']['region'] = trim($this->request->post['region']);
        }

        $this->response->setOutput(json_encode($json));
    }

}
?>
